package com.yuhs.zixing.study.design.pattern.dp001.principle.p06.demeter;

import java.util.ArrayList;
import java.util.List;

/**
 * @description
 * @author: yuhs
 * @date 2022/3/26 下午9:42
 */
public class DemeterDemo002 {
    public static void main(String[] args) {
        System.out.println("----------使用迪米特法则改进----------------------");
        SchoolManager002 schoolManager = new SchoolManager002();
        schoolManager.printAllEmployee(new CollegeManager002());

    }
}

/**
 * 学校总部员工类
 */
class Employee002 {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

/**
 * 学院员工类
 */
class CollegeEmployee002 {
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

/**
 * 管理学院员工的管理类
 */
class CollegeManager002 {
    public List<CollegeEmployee002> getAllEmployee() {
        List<CollegeEmployee002> list = new ArrayList<>();
        CollegeEmployee002 employee002 = null;
        for (int i = 0; i < 10; i++) {
            employee002 = new CollegeEmployee002();
            employee002.setId("学校学院员工ID= " + i);
            list.add(employee002);
        }
        return list;
    }

    /**
     * 输出学院员工信息
     */
    public void printEmployee() {
        List<CollegeEmployee002> allEmployee = getAllEmployee();
        System.out.println("-------------学院员工开始------------------");
        allEmployee.forEach(collegeEmployee002 -> System.out.println(collegeEmployee002.getId()));
        System.out.println("-------------学院员工结束------------------");
    }
}

/**
 * 管理学院员工的管理类
 */
class SchoolManager002 {
    public List<Employee002> getAllEmployee() {
        List<Employee002> list = new ArrayList<>();
        Employee002 employee002 = null;
        for (int i = 0; i < 10; i++) {
            employee002 = new Employee002();
            employee002.setId("学校总部员工ID= " + i);
            list.add(employee002);
        }
        return list;
    }

    /**
     * 输出学校总部和学院的信息
     *
     * @param collegeManager002
     */
    public void printAllEmployee(CollegeManager002 collegeManager002) {
        // 分析问题
        // 1、这里 CollegeManager002 不是 SchoolManager002 的直接朋友
        // 2、CollegeManager002 是以局部变量方式出现在 SchoolManager002
        // 3、违反了迪米特法则
        collegeManager002.printEmployee();

        List<Employee002> employee002List = this.getAllEmployee();
        System.out.println("-------------总部员工开始------------------");
        employee002List.forEach(employee002 -> System.out.println(employee002.getId()));
        System.out.println("-------------总部员工结束------------------");
    }
}

